package com.jydd.singlekotlinmvvm.comm.base

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 * created by 韦敏敏
 * on 2020/1/20
 */
open class BaseDataSource {
    suspend fun <T>  request(
        call: suspend CoroutineScope.() -> T,
        onError: suspend CoroutineScope.(error: Exception) -> Unit
    ) : T? {
        return withContext(Dispatchers.IO){
            try {
                call()
            } catch (e: Exception) {
                e.printStackTrace()
                onError(e)
                null
            }
        }
    }
}